package com.travelsky.airchina.boarding.dao.impl.hibernate;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.travelsky.airchina.boarding.dao.ProductImageDAO;
import com.travelsky.airchina.boarding.domain.ProductImage;


@Repository("productImageDAO")
public class ProductImageDAOImpl extends GenericDAOHibernate<ProductImage, Long,ProductImageDAO> implements ProductImageDAO{
	@Autowired
	public ProductImageDAOImpl(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	public void deleteProductImageByProductId(Long id) {
		String hql = "delete from ProductImage where product.id = ? ";
		this.getHibernateTemplate().bulkUpdate(hql, id);
	}
	public void updateProductImage(Long id, String productImageToken) {
		String hql = "update ProductImage set product.id = ? where imageDesc = ? ";
		this.getHibernateTemplate().bulkUpdate(hql, new Object[]{id,productImageToken});
		String hql0 = "update ProductImage set imageDesc = '' where product.id = ? ";
		this.getHibernateTemplate().bulkUpdate(hql0, id);
	}
	public boolean isExistProductImage(Long id){
		String hql = "select count(*) from ProductImage where id = :productImageId ";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("productImageId", id);
		int count = this.countHqlQuery(hql, params);
		if(count>0){
			return true;
		}
		return false;
	}
}
